package com.nuttysoft.nutand.views;

import android.app.FragmentManager;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.nuttysoft.nutand.R;
import com.nuttysoft.nutand.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/9 0009.
 */
public class BottomTabView extends LinearLayout {

    private Context context;

    /**显示的item内容*/
    private UnScrollViewPager mViewPager;

    private MyFragmentPagerAdapter mFragmentPagerAdapter;

    private LinearLayout bottomTabCotainer;

    private List<Fragment> fragments;

    private LayoutParams itemParams;

    private List pressedImgResIDs = new ArrayList();

    private List unpressedImgResIDs = new ArrayList();

    private List<String> itemTitles = new ArrayList();

    private List<TabItem> tabItems = new ArrayList();

    private int pressedColor;

    private int unpressedColor;

    public BottomTabView(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public BottomTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    public BottomTabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    public void initView(){

        this.setOrientation(LinearLayout.VERTICAL);

        itemParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,1);
        itemParams.setMargins(0, (int)ViewUtil.dip2px(context,8),0,(int)ViewUtil.dip2px(context,8));
        LayoutParams contentParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1);
        LayoutParams bottomParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        mViewPager = new UnScrollViewPager(context);
        mViewPager.setId(R.id.index_viewpaper);
        mViewPager.setOffscreenPageLimit(4);
        if((this.context instanceof FragmentActivity)){
            android.support.v4.app.FragmentManager mFragmentManager = ((FragmentActivity)this.context).getSupportFragmentManager();
            mFragmentPagerAdapter = new MyFragmentPagerAdapter(mFragmentManager);
            mViewPager.setAdapter(mFragmentPagerAdapter);
        }

        bottomTabCotainer = new LinearLayout(context);
        bottomTabCotainer.setOrientation(LinearLayout.HORIZONTAL);

        this.addView(mViewPager, contentParams);
        this.addView(bottomTabCotainer, bottomParams);
    }

    /**
     * 添加相互关联的fragment
     * @param fragments 要添加的fragments
     * */
    public void addFragmentList(List<Fragment> fragments){
        this.fragments = fragments;
        mFragmentPagerAdapter.notifyDataSetChanged();
        mViewPager.setCurrentItem(0);
    }

    /**
     * @param titles 要设置的底部文字，顺序添加
     *
     * */
    public void addBottomTitle(List<String> titles){
        this.itemTitles = titles;
    }

    /**
     * 设置选中的图片
     * @param imgResIDs 要设置的底部文字，顺序添加
     *
     * */
    public void addBottomPressImg(List imgResIDs){
        this.pressedImgResIDs = imgResIDs;
    }

    /**
     * 设置未选中的图片
     * @param imgResIDs 要设置的底部文字，顺序添加
     *
     * */
    public void addBottomUnpressImg(List imgResIDs){
        this.unpressedImgResIDs = imgResIDs;
    }

    public void setBottomTitlePressedColor(int hexClr){
        this.pressedColor =  hexClr;
    }

    public void setBottomTitleUnpressedColor(int hexClr){
        this.unpressedColor =  hexClr;
    }

    /**
     * 设置背景色
     * */
    public void setBackBottmBg(int resId){
        bottomTabCotainer.setBackgroundResource(resId);
    }

    /**
     * 最后务必调用
     * 暂时没必要采取构建着模式
     * */
    public void init(){

        if(pressedImgResIDs.size()== unpressedImgResIDs.size()&& pressedImgResIDs.size()== itemTitles.size()){

            for(int i = 0 ; i < unpressedImgResIDs.size();i ++){
                TabItem tabItem = new TabItem(context);
                tabItem.setText(itemTitles.get(i));
                tabItem.setimgRes((Integer) unpressedImgResIDs.get(i));
                tabItem.setTag(R.id.bottom_id, i);
                tabItem.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        setCurrentItem((Integer) v.getTag(R.id.bottom_id));
                    }
                });
                bottomTabCotainer.addView(tabItem, itemParams);
                tabItems.add(tabItem);
            }
        }else{
            Log.d("Bottom","图片个数和标题不匹配");
        }
    }

    public void setCurrentItem(int index){
        clearAllChecked();
        tabItems.get(index).setimgRes((Integer) pressedImgResIDs.get(index));
        tabItems.get(index).setTextColor(pressedColor);
        mViewPager.setCurrentItem(index,false);
    }

    public void clearAllChecked(){

        for(int i= 0; i < tabItems.size();i++){
            tabItems.get(i).setimgRes((Integer) unpressedImgResIDs.get(i));
            tabItems.get(i).setTextColor(unpressedColor);
        }
    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        public MyFragmentPagerAdapter(android.support.v4.app.FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments==null?null:fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments==null?0:fragments.size();
        }
    }
}
